package com.ybwh.springboot1.common;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * ajax 返回给前端的数据 模型
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespData<T> {

	/**
	 * 成功
	 */
	public static final int SUCCEED_CODE = 1;
	/**
	 * 失败
	 */
	public static final int FAIL_CODE = 0;

	/**
	 * 重新登录
	 */
	public static final int RE_LOGIN_CODE = -1;

	/**
	 * 错误码
	 */
	private int code;

	/**
	 * 错误描述
	 */
	private String msg;

	/**
	 * 返回数据
	 */
	private T data;

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
	}

	public static <E> RespData<E> successResp(E data) {
		return new RespData<E>(SUCCEED_CODE, null, data);
	}

	public static <E> RespData<E> failedResp(String msg) {
		return new RespData<E>(FAIL_CODE, msg, null);
	}


	public static <E> RespData<E> reLoginResp(String msg) {
		return new RespData<E>(RE_LOGIN_CODE, msg, null);
	}



}